﻿using System;

namespace IupNET
{
    public class CanvasState:IDisposable
    {
        internal IntPtr saveobj = IntPtr.Zero;
        private bool disposed = false;
        //TODO: this class crashes in destructor....

        
        internal CanvasState(IntPtr saveobj)
        {
            this.saveobj = saveobj;
        }

        ~CanvasState()
        {
            Dispose(false);
        }
       

        public void Dispose()
        {

            GC.SuppressFinalize(this);
            Dispose(true);
        }

        //static int cnt = 0;

        public void Dispose(bool disposing)
        {
           // cnt++;
            
            if (!disposed)
            {
                if (saveobj != IntPtr.Zero)
                {
                    try
                    {
                        Cdc.ReleaseState(saveobj);
                    }
                    catch(Exception) {
                        //die silently. It seems like a bug in cd with an acess violation. 
                        //Iupc.Message("", "crash bug");
                    }
                    saveobj = IntPtr.Zero;
                }
            }
            disposed = true;
        
        }

      
    }
}
